﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using WhiteBoard;
using System.IO;
using System.Runtime.InteropServices;

namespace WFFS
{
    public partial class FrmSendAlldirective : Form
    {
          private Form1 frmMain = null;
     private FrmAlldirective frmAlldirective = null;
        public FrmSendAlldirective(Form1 _frm ,FrmAlldirective _frmAlldirective)
        {
            InitializeComponent();
            frmMain = _frm;
            frmAlldirective = _frmAlldirective;
            getInfo();
        }
     private void getInfo()
        {
            userList.Clear();
            userList.BeginUpdate();
            #region 增加Item的標題，共有三個列
            userList.View = View.Details;
            userList.Columns.Add("姓名", 80);
            userList.Columns.Add("中队", 60);
            userList.Columns.Add("支队", 60);
            userList.Columns.Add("职位", 60);
            userList.Columns.Add("IP", 100);
            #endregion
            string sql1 = "select userName,FireBrigade,FireTeam,DutyName,ipaddr from UserInfo where RFID<>'0000' ";
            SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                string userName = reader1["userName"].ToString();
                string fireTeam = reader1["FireTeam"].ToString();
                string DutyName = reader1["DutyName"].ToString();
                string ipaddr = reader1["ipaddr"].ToString();
                string fireBrigade = reader1["FireBrigade"].ToString();
                userList.Items.Add(userName, userName, 0);
                userList.Items[userName].SubItems.Add(fireBrigade);
                userList.Items[userName].SubItems.Add(fireTeam);
                userList.Items[userName].SubItems.Add(DutyName);
                userList.Items[userName].SubItems.Add(ipaddr);
            }
            reader1.Close();
            userList.EndUpdate();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmSendAlldirective", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmSendAlldirective", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }

        private void button1_Click(object sender, EventArgs e){
            if (frmMain.netWork != null && userList.Items.Count > 0){
                Hashtable YesipTable = new Hashtable(); //允许接收全息指令列表
                foreach (ListViewItem item in userList.Items){
                    String ip = item.SubItems[4].Text;
                  if (item.Checked) {
                    YesipTable.Add(ip, ip);
                   } 
                }
                WBMsgAllDirective wn= frmAlldirective.createImage();
                frmMain.netWork.SendWBMsgConnectedHdlr(wn, YesipTable);
                MessageBox.Show("下达成功");
                this.Hide();
            }
        }

        private void FrmNegotiation_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmNegotiation", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmNegotiation", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
